<?php
/**
 * 作品打印，对接python，
 * 将参数转换成适合python处理数据
 * Created by PhpStorm.
 * User: hjlan
 * Date: 2016年7月26日
 * Time: 9:59
 *
 */

namespace App\Services\PrintService;
use App\Jobs\PrintDataSaveQueue;
use App\Services\ImgServices\ImgTool;
use App\Services\SourcePicService\SourcePicTool;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\ImageProcessor\ImagickTool;
use App\Services\Tool\Svg\SVGFont;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
class PrintService
{
    private $image_tool;
    /**
     * PrintService constructor.
     */
    public function __construct()
    {
    }

    /***
     *队列推送事件
     * @param array $products
     * @return bool
     */
    public function job2py(array $products){
        try{
            foreach ($products as $product){ //产品数组
                if(!is_array($product)){
                    Log::info('..........job2py $product不是数组..........');
                    continue;
                }
                $product_id=$product['product_id'];
                $product_print_materials=$product['product_print_materials'];
                foreach ($product_print_materials as $print_material){ //每个打印区域
                    //各个打印区域
                    $print_material_res=$this->productPrintMaterial2py($print_material,$product_id);
                }
            }
            return true;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 每个设计区域进行数据处理，调取处理程序
     * @param array $print_material
     * @return bool
     */
    private function productPrintMaterial2py(array $print_material,$product_id){
        try{
            $product_print_id=$print_material['product_print_id'];
            $position_type=$print_material['position_type'];
            $product_print_materials_ids=$print_material['product_print_materials_ids'];
            $product_preview_image_id=$print_material['product_preview_image_id'];
            $res=$this->printImagesGenerated($product_print_materials_ids,$product_print_id,$product_preview_image_id,$product_id);
            return $res;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 打印参数处理
     * @param $product_print_materials_ids
     * @param $product_print_id
     * @param $product_preview_image_id
     * @return bool
     */
    public function printImagesGenerated($product_print_materials_ids,$product_print_id,$product_preview_image_id,$product_id){
        try{
            //打印通用参数
            if (!is_array($product_print_materials_ids)||!is_numeric($product_print_id)||!is_numeric($product_preview_image_id)){
                Log::info('************** 调取 printImagesGenerated 出错 ************');
                $data['product_print_materials_ids']=$product_print_materials_ids;
                $data['product_print_id']=$product_print_id;
                $data['product_preview_image_id']=$product_preview_image_id;
                Log::info($data);
                return false;
            }
            $product_print_id=intval($product_print_id);
            $product_id=intval($product_id);
            //获取绘制通用参数
            $product_print=$this->getProductPrintById($product_print_id);
            if(empty($product_print)){
                Log::info('************ printImagesGenerated 获取通用参数(getProductPrintById)出错*************');
                return false;
            }
            $product_print_materials=$this->getProductPrintMaterialsByIds($product_print_materials_ids);
            if(empty($product_print)){
                Log::info('************ printImagesGenerated getProductPrintMaterialsByIds出错*************');
                return false;
            }
            // 创作客户端版本号
            $app_version=empty($product_print->app_version)?'':$product_print->app_version;
            $styleBoxPadW=$product_print->style_box_w;
            $styleBoxPadH=$product_print->style_box_h;
            $product_temp_id=$this->getProductTempIdByProductId($product_id);
            $res=$this->php2python($product_print_materials,$styleBoxPadW,$styleBoxPadH,$product_temp_id,$product_preview_image_id,$app_version);
            return $res;
        }catch (\Exception $e){
            Log::info(' printImagesGenerated 出现异常');
            Log::info($e);
            return false;
        }
    }

    /**
     * php 给python传值
     * @param $product_print_materials
     * @param $styleBoxPadW
     * @param $styleBoxPadH
     * @param $product_temp_id
     * @param $product_preview_image_id  预览图数据库Id
     * @return bool
     */
    public function php2python($product_print_materials,$styleBoxPadW,$styleBoxPadH,$product_temp_id,$product_preview_image_id,$app_version)
    {
        try{
            //放大倍数
            $multiple = 2520/ $styleBoxPadW;
            $boxW=round($styleBoxPadW*$multiple);
            $boxH=round($styleBoxPadH*$multiple);
            $data = $this->dataProcessing($product_print_materials, $multiple,$product_temp_id,$app_version,$boxW,$boxH);
            if($data===false){
                return false;
            }
            $url		= 'http://127.0.0.1:9999/createImg';
            $header[]	= "Content-type:application/json";//定义content-type为xml,注意是数组
            $images=array();
            $images['image_parm_arr']=$data;
            $images['boxW']=$boxW;
            $images['boxH']=$boxH;
            $images['dpi']=180;
            $path='product_print_images'.DIRECTORY_SEPARATOR.date('Ym').DIRECTORY_SEPARATOR;
            $folder=public_path("images").DIRECTORY_SEPARATOR.$path;
            @$this->mkFolder($folder);
            $file_name=md5(date('ymdhis')).'.png';
            $save_path=$folder.$file_name;
            $images['save_path']=$save_path;
            $json_obj=json_encode($images);
            $ch = curl_init ($url);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS, $json_obj);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60); //timeout in seconds
            $response = curl_exec($ch);
            curl_close($ch);//如果成功关闭请求
            $response_obj=json_decode($response);
            if(isset($response_obj->code)&&($response_obj->code==1)){
                if(file_exists($save_path)){
                    $file_md5=md5_file($save_path);
                    $new_path=$folder.$file_md5.'.png';
                    rename($save_path,$new_path);
                    $print_data['img_path']=$path.$file_md5.'.png';
                }
                $print_data['product_preview_image_id']=$product_preview_image_id;
                $this->saveProductPreviewImages($print_data);
            }
            return true;
        }catch (\Exception $e){
            Log::info(date("Y-m-d h:i:s", time()));
            Log::info('*********** PrintService.php php2python 异常 **************');
            Log::info($e);
            return false;
        }
    }

    /**
     * 制作素材获取
     * @param $product_print_materials
     * @param $multiple
     * @param $product_temp_id   产品模板ID
     * @param $app_version       创作客户端软件版本号
     * @return array
     */
    public function dataProcessing($product_print_materials, $multiple,$product_temp_id,$app_version,$boxW,$boxH)
    {
        try{
            $datas = array();
            //按照edit_view_level从小到大排序
            usort($product_print_materials, function($a, $b) {
                $al = $a->edit_view_level;
                $bl = $b->edit_view_level;
                if ($al == $bl)
                    return 0;
                return ($al > $bl) ? 1 : -1;
            });
            foreach ($product_print_materials as $product_print_material) {
                $product_print_material->image_finnal_size_width = isset($product_print_material->image_finnal_size_width) ? round($product_print_material->image_finnal_size_width*$multiple) : 0;//在ios上的素材大小
                $product_print_material->image_finnal_size_height = isset($product_print_material->image_finnal_size_height) ? round($product_print_material->image_finnal_size_height*$multiple) : 0;
                if(!empty($app_version)&&GeneralTool::compare_versions($app_version) >=320){
                    //改成中心点坐标系
                    $product_print_material->center_point_offset_y = round($product_print_material->center_point_offset_y*$multiple+$boxH/2);
                    $product_print_material->center_point_offset_x = round($product_print_material->center_point_offset_x*$multiple+$boxW/2);
                    $product_print_material->image_offset_x = round(($product_print_material->center_point_offset_x-$product_print_material->image_finnal_size_width/2));
                    $product_print_material->image_offset_y = round(($product_print_material->center_point_offset_y-$product_print_material->image_finnal_size_height/2));

                    $product_print_material->mask_center_point_offset_x = isset($product_print_material->mask_center_point_offset_x) ? ($product_print_material->mask_center_point_offset_x*$multiple+$product_print_material->image_finnal_size_width/2) : 0;
                    $product_print_material->mask_center_point_offset_y = isset($product_print_material->mask_center_point_offset_y) ? ($product_print_material->mask_center_point_offset_y*$multiple+$product_print_material->image_finnal_size_height/2) : 0;
                }else{
                    $product_print_material->image_offset_x = $product_print_material->center_point_offset_x-$product_print_material->image_finnal_size_width/2;
                    $product_print_material->image_offset_y = $product_print_material->center_point_offset_y-$product_print_material->image_finnal_size_height/2;//偏移的位置
                    $product_print_material->image_offset_y = round($product_print_material->image_offset_y*$multiple);
                    $product_print_material->image_offset_x = round($product_print_material->image_offset_x*$multiple);
                    $product_print_material->center_point_offset_y = round($product_print_material->center_point_offset_y*$multiple);
                    $product_print_material->center_point_offset_x = round($product_print_material->center_point_offset_x*$multiple);
                    $product_print_material->mask_center_point_offset_x = isset($product_print_material->mask_center_point_offset_x) ? ($product_print_material->mask_center_point_offset_x*$multiple) : 0;
                    $product_print_material->mask_center_point_offset_y = isset($product_print_material->mask_center_point_offset_y) ? ($product_print_material->mask_center_point_offset_y*$multiple) : 0;
                }
                $product_print_material->mask_finnal_size_width = isset($product_print_material->mask_finnal_size_width) ? round($product_print_material->mask_finnal_size_width*$multiple) : 0;//在ios上的素材大小
                $product_print_material->mask_finnal_size_height = isset($product_print_material->mask_finnal_size_height) ? round($product_print_material->mask_finnal_size_height*$multiple) : 0;
               if($product_print_material->type==1){
                    //图片
                    $new_product_print_material=$this->processImageData($product_print_material,$multiple,$product_temp_id);
                    if(empty($product_print_material)){
                        Log::info('处理图片数据出错..............');
                        return false;
                    }
                }else{
                    $new_product_print_material=$this->processFontData($product_print_material,$multiple,$app_version);
                    if(empty($new_product_print_material)){
                        Log::info('处理字体数据出错..............');
                        return false;
                    }
                    //字体
                }
                $product_print=array();
                $product_print['isHorizontalFilp']=$new_product_print_material->is_horizontal_filp;
                $product_print['isVerticalFilp']=$new_product_print_material->is_vertical_filp;
                $product_print['type']=$new_product_print_material->type;
                $product_print['imgW']=round($new_product_print_material->image_finnal_size_width);
                $product_print['imgH']=round($new_product_print_material->image_finnal_size_height);
                $product_print['imgY1']=round($new_product_print_material->image_offset_x);
                $product_print['imgX1']=round($new_product_print_material->image_offset_y);
                $product_print['imgPositonX']=round($new_product_print_material->center_point_offset_x);
                $product_print['imgPositonY']=round($new_product_print_material->center_point_offset_y);
                $product_print['angle']=-round($new_product_print_material->angle);
                $product_print['multiple']=$multiple;//需要算
                if($new_product_print_material->type==1){
                    $product_print['img_path']=$new_product_print_material->image_absolute_path;
                }else{
                    $product_print['font_path']=$new_product_print_material->font_path;
                    $product_print['font_color']=$new_product_print_material->font_color;
                    $product_print['text']=$new_product_print_material->text;
                    $product_print['font_size']=round($new_product_print_material->font_size);
                }
                $datas[]=(object)$product_print;
            }
            return $datas;
        }catch (\Exception $e){
            Log::info(date("Y-m-d h:i:s", time()));
            Log::info('*********** PrintService.php dataProcessing 异常 **************');
            Log::info($e);
            return array();
        }
    }

    /***
     * 处理图片数据
     * @param $product_print_material
     * @param $multiple
     * @return bool
     */
    private function processImageData($product_print_material,$multiple,$product_temp_id){
        try{
            $product_print_material->angle = rad2deg($product_print_material->angle);
            if($product_print_material->image_type == 2){//来自素材库
                $source_pic_result = SourcePicTool::getSourcePicAvailableStateFuncBySourcePicIdAndProdTempId($product_print_material->creative_id,$product_temp_id,1);
                if($source_pic_result['state'] != 'M00000'){
                    Log::info('素材问题：'.$source_pic_result['state']);
                    Log::info('素材问题：'.$source_pic_result['message']);
                    return false;
                }
                $product_print_material->original_image = $source_pic_result['result']->original_image;
            }else{//用户素材
                $user_creative=DB::table('user_creatives')
                    ->where(array('creative_id'=>$product_print_material->creative_id))
                    ->first();
                if(empty($user_creative)){
                    Log::info('用户素材编号为:'.$product_print_material->creative_id.'，素材不存在');
                    return false;
                }
                $product_print_material->original_image = $user_creative->original_image;
            }
            $product_print_material_absolute_path = ImgTool::getImgStoreAbsolutePathByImgPath($product_print_material->original_image);
            $product_print_material->image_absolute_path=$product_print_material_absolute_path;
            if(!file_exists($product_print_material_absolute_path)){
                Log::info('地址为：'.$product_print_material_absolute_path.',图片不存在');
                return false;
            }
            if(isset($product_print_material->mask_id) && $product_print_material->mask_id != 0){
                $mask_original_image = DB::table('masks')->where(['mask_id'=>$product_print_material->mask_id,'state'=>1])->value('original_image');
                $mask_image_absolute_path=ImgTool::getImgStoreAbsolutePathByImgPath($mask_original_image);
                if (!file_exists($mask_image_absolute_path)){
                    Log::info('遮罩原图不存在:'.$mask_image_absolute_path);
                    return false;
                }
                $mask_image_path =ImagickTool::generateMaskImg2(
                    $product_print_material_absolute_path,
                    $product_print_material->image_finnal_size_width,
                    $product_print_material->image_finnal_size_height,
                    $mask_image_absolute_path,
                    $product_print_material->mask_finnal_size_width,
                    $product_print_material->mask_finnal_size_height,
                    $product_print_material->image_offset_x ,
                    $product_print_material->image_offset_y,
                    $product_print_material->mask_center_point_offset_x,
                    $product_print_material->mask_center_point_offset_y,
                    $multiple,
                    180
                );
                $product_print_material->image_absolute_path=ImgTool::getImgStoreAbsolutePathByImgPath($mask_image_path);
            }
            return $product_print_material;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 处理字体数据
     * @param $product_print_material
     * @param $multiple
     * @param $app_version  创作客户端软件版本号
     * @return bool
     */
    private function processFontData($product_print_material,$multiple,$app_version){
        try{
            if(empty($product_print_material)){
                Log::info('processFontData $product_print_material为空 。。。。。。。。');
                return false;
            }
            $product_print_material->angle = rad2deg($product_print_material->angle);
            $product_print_material->font_size =  $product_print_material->font_size*$multiple;
            $use_svg_font=((empty($app_version) || GeneralTool::compare_versions($app_version) < 312)
            || empty($product_print_material->font_svg));
            if($use_svg_font){
                $product_print_material = $this->processFont($product_print_material,$multiple);
            }else{
                $product_print_material=$this->processFontImage($product_print_material,$multiple);
            }
            return $product_print_material;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 文本通过字体包绘制，参数处理
     * @param $product_print_material
     * @param $multiple
     * @return bool
     */
    private function processFont($product_print_material,$multiple){
        try{
            //获取支持中文的字体  如果不支持 直接提示 默认字体为1
            $font = DB::table('fonts')
                ->where([['font_id',$product_print_material->font_id]])
                ->select('font_path','font_name','font_type','font_image','state')
                ->first();
            if(empty($font) || !file_exists(public_path('fonts/').$font->font_path)){
                Log::info('processFontData 路径为:'.$font->font_path.'为空');
                return false;
            }
            $product_print_material->font_path = public_path('fonts/').$font->font_path;
            if(preg_match("/[\x7f-\xff]/", $product_print_material->text) && $font->font_type == 0){
                //不支持中文
                Log::info($font->font_name.'字体不支持中文');
                return false;
            }
            $product_print_material->font_name = $font->font_name;
            $product_print_material->font_image = $font->font_image;
            return $product_print_material;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 文本通过SVG绘制，参数处理
     * @param $product_print_material
     * @param $multiple
     * @return bool
     */
    private function processFontImage($product_print_material,$multiple){
        try{
            //生成svg
            if(empty($product_print_material->font_svg)){
                Log::info('svg文本为空：');
                return false;
            }
            $font_svg_json=json_decode($product_print_material->font_svg);
            if(!isset($font_svg_json->text_svg_info)){
                Log::info('font_svg json字符串,格式错误内容：');
                Log::info($product_print_material->font_svg);
                return false;
            }
            $font_svg_arr=(array)$font_svg_json;
            $svg_path = $this->generateSvg($font_svg_arr,$multiple);
            //将svg转换为png
            $image_tool=$this->getImageTool();
            $svg_absolute_path=public_path("images".DIRECTORY_SEPARATOR.'product_print_images').DIRECTORY_SEPARATOR.$svg_path;
            if(!file_exists($svg_absolute_path)){
                Log::info('路径为：'.$svg_absolute_path.',SVG不在');
                return false;
            }
            $image_png_path = $image_tool->svg2Png($svg_absolute_path,180,180);
            $image_absolute_path=public_path('images/'.$image_png_path);
            if(!file_exists($image_absolute_path)){
                Log::info('svg转换成图片不存在');
                return false;
            }
            $img_param_arr=ImagickTool::getImgParamByStateFuncAndImgPath($image_absolute_path);
            $img_width=$img_param_arr['width'];
            $img_height=$img_param_arr['height'];
            $product_print_material->image_finnal_size_width=$img_width;
            $product_print_material->image_finnal_size_height=$img_height;
            $product_print_material->type=1;
            $product_print_material->image_absolute_path=$image_absolute_path;
            return $product_print_material;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 获取放大倍数
     * @param $styleBoxPadW
     */
     public function getPrintMultiple($styleBoxPadW){
         //放大倍数
         $multiple = 2520/ $styleBoxPadW;
     }
    /**
     * 按照比例获取DPI
     * @param $xPint
     * @return int
     */
    public function getDpi($xPint){
        switch ($xPint)
        {
            case 1400:
                $dpi=100;
                break;
            case 1680:
                $dpi=120;
                break;
            case 2100:
                $dpi=150;
                break;
            case 2520:
                $dpi=180;
                break;
            case 3150:
                $dpi=225;
                break;
            case 4200:
                $dpi=300;
                break;
            default:
                $dpi=72;
        }
        return $dpi;
    }
    public function printProcessingFail()
    {
        $create_print_records=DB::table('create_print_records')->where('fail_code','<>',0)->select('create_id','is_front')->get();
    }

    /**
     * 根据失败作品id，生成图片
     * @param $create_id
     */
    public function printProcessingFailById($create_id){
        try{
            $images=array();
            $print_datas=DB::table('create_print_materials')->where(array('create_id'=>$create_id))->where('fail_code','<>',0)->get();
            foreach ($print_datas as $print_data){
                $image=array();
                $image['creative_id']=$print_data->creative_id;
                $image['centerPointOffsetX']=$print_data->centerPointOffsetX;
                $image['centerPointOffsetY']=$print_data->centerPointOffsetY;
                $image['angle']=$print_data->angle;
                $image['type']=$print_data->type;
                $image['imageType']=$print_data->imageType;
                $image['edit_view_level']=$print_data->edit_view_level;
                if($print_data->type==1){

                }elseif ($print_data->type==2){
                    $image['text']=$print_data->text;
                    $image['fontColor']=$print_data->fontColor;
                    $image['fontSize']=$print_data->fontSize;
                    $image['font_id']=$print_data->fontStyle;
                }
                $images[]=$image;
            }
            $create_print=DB::table('create_prints')->where(array('create_id'=>$create_id))->first();
            if(!empty($create_print)){
                $styleBoxPadW=$create_print->styleBoxPadW;
                $styleBoxPadH=$create_print->styleBoxPadH;
                $user_id=$create_print->user_id;
                $this->php2python($images,$styleBoxPadW,$styleBoxPadH,$user_id,$create_id,$create_print->is_front);
            }
            return true;
        }catch (\Exception $e){
            Log::info(date("Y-m-d h:i:s", time()));
            Log::info('*********** PrintService.php printProcessingFailById 异常 **************');
            Log::info($e);
            return false;
        }
    }

    /**
     * 创建文件夹
     * @param $path
     */
    function mkFolder($path)
    {
        try{
            if(!is_readable($path))
            {
                is_file($path) or mkdir($path,0666);
            }
        }catch (\Exception $e){
            Log::info('PrintService mkFolder 出现异常');
            Log::info($e);
            return false;
        }
    }

    /***
     * 获取制作区域通用参数
     * @param $id
     * @return null
     */
    private function getProductPrintById($id){
        if(!is_int($id)){
            return null;
        }
        return DB::table('product_prints')->where(array('id'=>$id))->first();
    }

    /***
     * 获取制作区域
     * @param array $ids
     * @return mixed
     */
    private function getProductPrintMaterialsByIds(array $ids){
        try{
            if(empty($ids)){
                return array();
            }
            $product_print_material_ids=array();
            foreach ($ids as $id) {
                $product_print_material_ids[]=intval($id);
            }
            $product_print_materials=DB::table('product_print_materials')
                ->whereIn('id',$product_print_material_ids)
                ->orderBy('edit_view_level','DESC')
                ->get()
                ->toArray();
            return $product_print_materials;
        }catch (\Exception $e){
            Log::info('getProductPrintMaterialsByIds *****************');
            Log::info($e);
            return array();
        }
    }

    /****
     * 产品ID获取产品模板ID
     * @param $product_id
     * @return string
     */
    private function getProductTempIdByProductId($product_id){
        $product_id=trim($product_id);
        if(empty($product_id)){
            return '';
        }
        $product_temp_id=DB::table('products')
            ->where('product_id',$product_id)
            ->value('product_temp_id');
        return empty($product_temp_id)?'':$product_temp_id;
    }

    /***
     * 保存打印图到数据库
     * @param array $data
     * @return bool
     */
    private function saveProductPreviewImages(array $data){
        $img_path=$data['img_path'];
        $preview_image['product_print_image']=$img_path;
        $preview_image['product_print_thumbnail_image']='';
        $preview_image['product_preview_image_id']=$data['product_preview_image_id'];
        if(!isset($preview_image['product_preview_image_id'])){
            Log::info('saveProductPreviewImages product_preview_image_id传入错误');
            return false;
        }
        $preview_image['updated_at']=time();
        $num=DB::table('product_preview_images')
            ->where(array('product_preview_image_id'=>$preview_image['product_preview_image_id']))
            ->update($preview_image);
        return ($num===0)?false:true;
    }
    /**
     * 生成svg文件
     * @param $svg_param
     * @param int $scale  放大倍数
     * @return string
     * @throws \Exception
     */
    public function generateSvg($svg_param,$scale=1){
        $svg = new SVGFont();
        $arr = $svg->get_array_to_use($svg_param,$scale);
        $svg->set_font_family($arr['font_family']);
        $svg->set_font_size($arr['font_size']);
        $svg->set_font_newline($arr['newline']);
        $svg->set_scale($arr['scale']);
        $svg->set_font_fill($arr['fill']);
        $svg->addText($arr['text']);
        $svg->addAttribute("width", ($svg->get_text_width() + $svg->get_x_begin())."px");
        $svg->addAttribute("height", $svg->get_text_height() . "px");
        $svg_path =  'svg' . DIRECTORY_SEPARATOR .date('Ym') .DIRECTORY_SEPARATOR ;
        $svg_name=md5(date('ymdhis').rand(1, 1000)) . ".svg";
        $folder=public_path("images".DIRECTORY_SEPARATOR.'product_print_images').DIRECTORY_SEPARATOR.$svg_path;
        @$this->mkFolder($folder);
        $svg_absolute_path=$folder.$svg_name;
        $result = $svg->asXML($svg_absolute_path);
        return $svg_path.$svg_name;
    }

    /***
     * 获取image_tool实例
     * @return ImagickTool
     */
    private function getImageTool(){
        $image_tool=$this->image_tool;
        if(empty($image_tool)){
            $image_tool=new ImagickTool();
            $this->image_tool=$image_tool;
        }
        return $image_tool;
    }
}

class strClass
{
}